using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;

namespace PlanetWPF.Toolbelt.Controls
{
    /// <summary>
    /// Holds the settings for a column in a <see cref="PowerGrid"/>.
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class PowerGridColumn : DependencyObject, INotifyPropertyChanged
    {
        /// <summary>
        /// Provides notification that a property has changed on this column.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #region Dependency Properties
        /// <summary>
        /// Holds the display text for the column.
        /// </summary>
        public static readonly DependencyProperty DisplayNameProperty;
        /// <summary>
        /// Holds the width for a column. This value is used to 
        /// determine how wide the column will be when rendered.
        /// </summary>
        public static readonly DependencyProperty WidthProperty;
        /// <summary>
        /// Holds the <see cref="PowerGridColumnType"/> for a column
        /// </summary>
        public static readonly DependencyProperty ColumnTypeProperty;
        /// <summary>
        /// Holds a string value representing the PropertyPath that 
        /// <see cref="PowerGridCell"/>s assigned to this column
        /// will use for binding.
        /// </summary>
        public static readonly DependencyProperty ColumnPathProperty;

        #endregion
        #region Static Constructor
        static PowerGridColumn()
        {
            DisplayNameProperty = DependencyProperty.Register("DisplayName", typeof(String), typeof(PowerGridColumn), new FrameworkPropertyMetadata("", DependencyPropChanged));
            WidthProperty = DependencyProperty.Register("Width", typeof(int), typeof(PowerGridColumn), new FrameworkPropertyMetadata(100, DependencyPropChanged));
            ColumnTypeProperty = DependencyProperty.Register("ColumnType", typeof(PowerGridColumnType), typeof(PowerGridColumn), new UIPropertyMetadata(PowerGridColumnType.Text));
            ColumnPathProperty = DependencyProperty.Register("ColumnPath", typeof(String), typeof(PowerGridColumn), new UIPropertyMetadata("", DependencyPropChanged));
        }
        #endregion
        #region CLR Properties

        /// <summary>
        /// Holds a string value representing the PropertyPath that 
        /// <see cref="PowerGridCell"/>s assigned to this column
        /// will use for binding.
        /// </summary>
        public String ColumnPath
        {
            get { return (String)GetValue(ColumnPathProperty); }
            set { SetValue(ColumnPathProperty, value); }
        }

        /// <summary>
        /// Holds the display text for the column.
        /// </summary>
        public String DisplayName
        {
            get { return (String)GetValue(DisplayNameProperty); }
            set
            {
                SetValue(DisplayNameProperty, value);

            }
        }

        /// <summary>
        /// Holds the width for a column. This value is used to 
        /// determine how wide the column will be when rendered.
        /// </summary>
        public int Width
        {
            get { return (int)GetValue(WidthProperty); }
            set { SetValue(WidthProperty, value); }
        }

        /// <summary>
        /// Holds the <see cref="PowerGridColumnType"/> for a column
        /// </summary>
        public PowerGridColumnType ColumnType
        {
            get { return (PowerGridColumnType)GetValue(ColumnTypeProperty); }
            set { SetValue(ColumnTypeProperty, value); }
        }

        #endregion

        #region Internal Logic
        /// <summary>
        /// Fires the PropertyChanged event.
        /// </summary>
        /// <param name="propName">
        /// The name of the property that has changed.
        /// </param>
        private void NotifyPropertyChanged(String propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        /// <summary>
        /// Callback function fired whenever a property of a PowerGridColumn has changed
        /// </summary>
        /// <param name="d">The column that has a changed property</param>
        /// <param name="e">A <see cref="DependencyPropertyChangedEventArgs"/>
        /// that provides information regarding the property that has changed
        /// </param>
        /// <remarks>
        /// This function is essentially used to fire the <see cref="PropertyChanged"/>
        /// event on the Column.
        /// </remarks>
        private static void DependencyPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PowerGridColumn tmpColumn = d as PowerGridColumn;
            if (tmpColumn != null)
            {
                tmpColumn.NotifyPropertyChanged(e.Property.Name);
            }
        }
        #endregion


    }

    /// <summary>
    /// Inherits <see cref="ObservableCollection{T}"/>to provide 
    /// a custom implementation for <see cref="PowerGridColumn"/>s.
    /// </summary>
    public class PowerGridColumnCollection : ObservableCollection<PowerGridColumn>
    {
    }


}
